package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.PmsProductCategory;
import com.itheima.mapper.PmsProductCategoryMapper;
import com.itheima.service.db.PmsProductCategoryService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


@DubboService
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {
    @Autowired
    PmsProductCategoryMapper pmsProductCategoryMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, Long parentId) {
        Page<PmsProductCategory> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategory::getParentId, parentId);
        Page<PmsProductCategory> pmsProductCategoryPage = pmsProductCategoryMapper.selectPage(page, lqw);
        return new PageBeanVo(pageNum, pageSize, pmsProductCategoryPage.getTotal(), pmsProductCategoryPage.getRecords());
    }

    @Override
    public void navStatus(Long[] ids, Integer navStatus) {
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.in(PmsProductCategory::getId, ids);
        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(lqw);
        if (CollUtil.isNotEmpty(pmsProductCategories)) {
            for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
                pmsProductCategory.setNavStatus(navStatus);
                pmsProductCategoryMapper.updateById(pmsProductCategory);
            }
        }
    }

    @Override
    public void showStatus(Long[] ids, Integer showStatus) {
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.in(PmsProductCategory::getId, ids);
        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(lqw);
        if (CollUtil.isNotEmpty(pmsProductCategories)) {
            for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
                pmsProductCategory.setShowStatus(showStatus);
                pmsProductCategoryMapper.updateById(pmsProductCategory);
            }
        }
    }

    @Override
    public PmsProductCategory selectById(Long id) {
        return pmsProductCategoryMapper.selectById(id);
    }

    @Override
    public PmsProductCategory create(PmsProductCategory pmsProductCategory) {
        pmsProductCategoryMapper.insert(pmsProductCategory);
        return pmsProductCategory;
    }

    @Override
    public List<PmsProductCategory> findParents() {
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategory::getLevel, 0);
        return pmsProductCategoryMapper.selectList(lqw);
    }

    @Override
    public List<PmsProductCategory> findByParentId(Long id) {
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategory::getParentId, id);
        return pmsProductCategoryMapper.selectList(lqw);
    }

    @Override
    public PmsProductCategory findbyId(Long id) {
        return pmsProductCategoryMapper.selectById(id);
    }

    @Override
    public void update(PmsProductCategory ppc) {
        pmsProductCategoryMapper.updateById(ppc);
    }

    @Override
    public void delete(Long id) {
        pmsProductCategoryMapper.deleteById(id);
    }


    @Override
    public List<PmsProductCategoryVo> findAll() {
        return selectByDG(0L);
    }

    public List<PmsProductCategoryVo> selectByDG(Long id){
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategory::getParentId,id);
        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(lqw);
        if (pmsProductCategories.isEmpty()){
            return null;
        }

        List<PmsProductCategoryVo> voList =new ArrayList<>();
        for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
            List<PmsProductCategoryVo> childrenVo = selectByDG(pmsProductCategory.getId());
            PmsProductCategoryVo pVo = new PmsProductCategoryVo();
            BeanUtils.copyProperties(pmsProductCategory, pVo);
            if (childrenVo!=null){
                pVo.setChildren(childrenVo);
            }
            voList.add(pVo);
        }

        return voList;
    }


}
